package com.powerunion.datacollection.web.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.powerunion.datacollection.service.ItemManager;
import com.powerunion.datacollection.transaction.Transaction;
import com.powerunion.datacollection.transaction.TransactionHelper;
import com.powerunion.util.StringUtils;
import com.powerunion.util.log.LogHelper;

public class ItemAction extends DispatchAction {

	private static final Logger logger = LogHelper.getInstance().getLogger(
			ItemAction.class);
	private ItemManager itemManager = null;

	public ItemAction() {
		super();
		this.itemManager = new ItemManager();
	}

	public void finalize() throws Throwable {
		this.itemManager = null;
		super.finalize();
	}

	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String saveStr = request.getParameter("formData");
		int itemType = Integer.parseInt(request.getParameter("itemType"));
		Transaction transaction = null;
		try {
			transaction = TransactionHelper.getTransaction();
			transaction.begin();
			logger.debug("ItemInfo信息定义:" + saveStr);

			String currentUser = request.getRemoteUser();
			String id = this.itemManager.save(saveStr, currentUser,itemType);

			if (id != null && !id.equals("")) {
				response.getWriter().write("{success:true,data:'" + id + "'}");
			} else {
				response.getWriter().write("{failure:true}");
			}

			transaction.commit();
		} catch (Exception e) {
			logger.error("", e);
			try {
				transaction.rollback();
			} catch (Exception e1) {
				logger.error("", e1);
				throw e;
			} finally {
				transaction = null;
				response.getWriter().write("{failure:true}");
			}
		}

		return null;
	}

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String queryStr = request.getParameter("queryStr");

		String startStr = request.getParameter("start") == null ? "0" : request
				.getParameter("start");
		String limitStr = request.getParameter("limit") == null ? "28"
				: request.getParameter("limit");

		int start = Integer.parseInt(startStr);
		int limit = Integer.parseInt(limitStr);

		String result = this.itemManager.getItemInfos(queryStr, start, limit);
		logger.debug("ItemInfo:" + result);
		logger.debug("queryStr:" + queryStr);
		response.setContentType("text/json;charset=utf-8");
		try {
			response.getWriter().print(result);
		} catch (Exception e) {
			logger.error("输出json失败", e);
			return null;
		}
		return null;
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String[] ids = StringUtils.split(request.getParameter("codes"), ",");

		Transaction transaction = null;

		response.setContentType("text/json;charset=utf-8");
		try {
			transaction = TransactionHelper.getTransaction();
			transaction.begin();

			boolean flag = this.itemManager.delete(ids);

			if (flag == true) {
				response.getWriter().print("{success:true}");
			} else {
				response.getWriter().write("{failure:true}");
			}
			transaction.commit();
		} catch (Exception e) {
			logger.error("", e);
			try {
				transaction.rollback();
			} catch (Exception e1) {
				logger.error("", e1);
				throw e1;
			} finally {
				transaction = null;
				response.getWriter().write("{failure:true}");
			}
		}
		return null;
	}

}
